<template>
  <el-form :model="itemForm" :rules="rules" ref="itemForm" label-width="100px" class="demo-itemForm">
    <el-form-item label="商品名称" prop="name">
      <el-input v-model="itemForm.name"></el-input>
    </el-form-item>
    <el-form-item label="条码" prop="barcode">
      <el-input v-model="itemForm.barcode"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="price">
      <el-input v-model="itemForm.price"></el-input>
    </el-form-item>
    <el-form-item label="商品类别" prop="type">
      <el-select v-model="itemForm.type" placeholder="商品类别">
        <el-option label="计件商品" value=0></el-option>
        <el-option label="计重商品" value=1></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="单位" prop="unit">
      <el-input v-model="itemForm.unit"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('itemForm')">商品添加</el-button>
      <el-button @click="resetForm('itemForm')">清空</el-button>
    </el-form-item>
  </el-form>

</template>
<script>
import itemService from "../global/service/itemService";

export default {
  name: "ItemCreate",
  data() {
    const validatePrice = (rule, value, callback) => {
      if (value === undefined || value === "") {
        callback(new Error('请输入价格'));
      }
      if (value === '0') {
        callback(new Error('价格不能为0'));
      }
      let regPos = /^((0{1}\.\d{1,2})|([1-9]\d*\.{1}\d{1,2})|([1-9]+\d*))$/;
      if (!regPos.test(value)) {
        callback(new Error('必须为数值'))
      }
      if (parseFloat(value) > 10000 || parseFloat(value) < 1) {
        callback(new Error('金额在 1 - 10000'))
      } else {
        callback();
      }
    };

    const validateName = (rule, value, callback) => {
      if (value === undefined || value === "") {
        callback(new Error('请输入商品名称'));
      } else {
        if (value.length > 20) {
          callback(new Error('长度为 1 - 20 个字符'));
        } else {
          callback();
        }
      }
    };

    const validateBarcode = (rule, value, callback) => {
      if (value === undefined || value === "") {
        callback(new Error('请输入商品条码'));
      } else {
        if (value.length > 20 || value.length < 5) {
          callback(new Error('长度为 5 - 20 个字符'));
        } else {
          callback();
        }
      }
    };

    const validateUnit = (rule, value, callback) => {
      if (value.length > 5) {
        callback(new Error('长度不超过 5 个字符 '));
      } else {
        callback();
      }
    };

    return {
      itemForm: {
        unit: "",
      },
      rules: {
        name: [
          {required: true, validator: validateName, trigger: 'blur'},
        ],
        barcode: [
          {required: true, validator: validateBarcode, trigger: 'blur'},
        ],
        price: [
          {required: true, validator: validatePrice, trigger: 'blur'},
        ],
        type: [
          {required: true, message: '请选择商品类型', trigger: 'change'},
        ],
        unit: [
          {max: 5, validator: validateUnit, trigger: 'blur'}
        ],
      }
    };
  },
  methods: {
    async extracted(param) {
      await itemService.create(param);
      this.$message.success('添加成功!')
      this.$router.go(-1)
    },
    submitForm(itemForm) {
      const param = {
        name: this.itemForm.name,
        barcode: this.itemForm.barcode,
        price: this.itemForm.price,
        type: this.itemForm.type,
        unit: this.itemForm.unit
      };
      this.$refs[itemForm].validate((valid) => {
        if (valid) {
          this.extracted(param)
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(itemForm) {
      this.$refs[itemForm].resetFields();
    },

  }

}
</script>
